package pe.gob.osinergmin.activity.generic;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import pe.gob.osinergmin.R;
import pe.gob.osinergmin.activity.dialog.ExitDialog;
import pe.gob.osinergmin.service.GuiaFiscalizacionDetalleService;
import pe.gob.osinergmin.service.OrdenPedidoService;
import pe.gob.osinergmin.service.ProductoService;
import pe.gob.osinergmin.service.SupervisionMovilDetalleScopService;
import pe.gob.osinergmin.service.SupervisionMovilDetalleSeguridadService;
import pe.gob.osinergmin.service.SupervisionMovilService;
import pe.gob.osinergmin.service.UnidadOperativaService;
import pe.gob.osinergmin.service.impl.GuiaFiscalizacionDetalleServiceImpl;
import pe.gob.osinergmin.service.impl.OrdenPedidoServiceImpl;
import pe.gob.osinergmin.service.impl.ProductoServiceImpl;
import pe.gob.osinergmin.service.impl.SupervisionMovilDetalleScopServiceImpl;
import pe.gob.osinergmin.service.impl.SupervisionMovilDetalleSeguridadServiceImpl;
import pe.gob.osinergmin.service.impl.SupervisionMovilServiceImpl;
import pe.gob.osinergmin.service.impl.UnidadOperativaServiceImpl;
import pe.gob.osinergmin.utils.PuntoControlGPSTracker;


public abstract class GenericActionBarActivity extends ActionBarActivity implements View.OnClickListener{

    private Toolbar toolbar;
    private static PuntoControlGPSTracker puntoControlGPSTracker;
    private static ProductoService productoService;
    private static UnidadOperativaService unidadOperativaService;
    private static GuiaFiscalizacionDetalleService guiaFiscalizacionDetalleService;
    private static OrdenPedidoService ordenPedidoService;
    private static SupervisionMovilService supervisionMovilService;
    private static SupervisionMovilDetalleSeguridadService supervisionMovilDetalleSeguridadService;
    private static SupervisionMovilDetalleScopService supervisionMovilDetalleScopService;

    private static boolean doubleBackToExitPressedOnce = false;
    private ExitDialog dialog;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        puntoControlGPSTracker = new PuntoControlGPSTracker(this);
        productoService = ProductoServiceImpl.getInstance();
        unidadOperativaService = UnidadOperativaServiceImpl.getInstance();
        guiaFiscalizacionDetalleService = GuiaFiscalizacionDetalleServiceImpl.getInstance();
        ordenPedidoService = OrdenPedidoServiceImpl.getInstance();
        supervisionMovilService = SupervisionMovilServiceImpl.getInstance();
        supervisionMovilDetalleSeguridadService = SupervisionMovilDetalleSeguridadServiceImpl.getInstance();
        supervisionMovilDetalleScopService = SupervisionMovilDetalleScopServiceImpl.getInstance();
    }

    public Toolbar getToolbar(){
        return this.toolbar;
    }

    public void setToolbar(Toolbar toolbar){
        this.toolbar = toolbar;
    }

    public void onBackPressed() {
        //moveTaskToBack(true);
        showExitDialog();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //moveTaskToBack(true);
            showExitDialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showExitDialog(){
        if(dialog != null){
            if(!dialog.isShowing()){
                if(doubleBackToExitPressedOnce) {
                    dialog.show();
                }
                this.doubleBackToExitPressedOnce = true;

                showToast(this, R.string.confirm_back, Toast.LENGTH_SHORT);
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        doubleBackToExitPressedOnce=false;
                    }
                }, 2000);
            }
        }else{
            dialog = new ExitDialog(this);
            showExitDialog();
        }
    }

    public PuntoControlGPSTracker getPuntoControlGPSTracker(){
        return this.puntoControlGPSTracker;
    }

    public ProductoService getProductoService(){
        return this.productoService;
    }

    public GuiaFiscalizacionDetalleService getGuiaFiscalizacionDetalleService() {
        return this.guiaFiscalizacionDetalleService;
    }

    public OrdenPedidoService getOrdenPedidoService(){
        return this.ordenPedidoService;
    }

    public UnidadOperativaService getUnidadOperativaService() {
        return this.unidadOperativaService;
    }

    public SupervisionMovilService getSupervisionMovilService() {
        return this.supervisionMovilService;
    }

    public SupervisionMovilDetalleScopService getSupervisionMovilDetalleScopService(){
        return this.supervisionMovilDetalleScopService;
    }

    public SupervisionMovilDetalleSeguridadService getSupervisionMovilDetalleSeguridadService(){
        return this.supervisionMovilDetalleSeguridadService;
    }

    public void showToast(Context context, int res_id, int duration){
        showToast(context, context.getResources().getString(res_id), duration);
    }

    public void showToast(Context context, CharSequence text, int duration){
        LayoutInflater inflater = getLayoutInflater();

        View custom_toast = inflater.inflate(R.layout.toast_custom, null);

        TextView textView = (TextView)custom_toast.findViewById(R.id.toast_text);
        textView.setText(text);

        Toast toast = new Toast(getApplicationContext());
        toast.setView(custom_toast);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 20);
        toast.setDuration(duration);
        toast.show();
    }

}
